//
//  Team9EmptyTool.m
//  Team9Project
//
//  Created by 朱嘉祺 on 15/10/23.
//  Copyright © 2015年 apple. All rights reserved.
//

#import "Team9EmptyTool.h"
#import "Team9Header.h"
#import <MJRefresh.h>
#import <UIScrollView+EmptyDataSet.h>
//#import "AppDelegate.h"
#import <AFNetworking.h>


@interface Team9EmptyTool ()<DZNEmptyDataSetDelegate,DZNEmptyDataSetSource>



@end

@implementation Team9EmptyTool


- (instancetype)initWithScrollView:(UIScrollView *)scrollView{

    self = [super init];
    if (self) {
        scrollView.emptyDataSetDelegate = self;
        scrollView.emptyDataSetSource = self;
        self.scrollView = scrollView;
        AFNetworkReachabilityManager *manger = [AFNetworkReachabilityManager sharedManager];
        self.loading = manger.networkReachabilityStatus;
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(changeNet) name:@"changeNet" object:nil];
    }
    return self;
}
- (void)dealloc{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}
- (void)changeNet{
    AFNetworkReachabilityManager *manger = [AFNetworkReachabilityManager sharedManager];
    self.loading = manger.networkReachabilityStatus;
    [self.scrollView reloadEmptyDataSet];
    if (self.loading) {
        [self.scrollView.header beginRefreshing];
        [self.scrollView.footer resetNoMoreData];
    } else [self.scrollView.header endRefreshing];
    [self.scrollView reloadEmptyDataSet];
}
+ (Team9EmptyTool *)toolWithScrollView:(UIScrollView *)scrollView{
    Team9EmptyTool *tool = [[Team9EmptyTool alloc]initWithScrollView:scrollView];
    return tool;
}
/*
 初始化工具类之后 有一个loading 这个是刷新的动画 是否播放
 afn 监听网络状态改变loading
 刷新的block 把loading 改变
 重写loading set方法, loading yes 的时候刷新scrollview的emptyset
 需要刷新数据的地方 调用 header
 调用header的时候调用 loading的set方法
 让占位图和刷新 状态一致
 header -> setLoading
 setLoading -> reloadEmptySet ->header
 注意不要产生递归现象
 方法需要自己看demo
 */
- (void)setLoading:(BOOL)loading
{
    if (self.isLoading == loading) {
        return;
    }
    _loading = loading;
    if (loading == YES) {
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(3 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            self.loading = NO;
        });
    }
    [self.scrollView reloadEmptyDataSet];
}
#pragma mark 背景字
- (NSAttributedString *)titleForEmptyDataSet:(UIScrollView *)scrollView
{
    NSString *text = nil;
    UIFont *font = nil;
    NSMutableDictionary *attributes = [NSMutableDictionary new];
    NSMutableParagraphStyle *paragraph = [NSMutableParagraphStyle new];
    paragraph.lineBreakMode = NSLineBreakByWordWrapping;
    paragraph.alignment = NSTextAlignmentCenter;
    AFNetworkReachabilityManager *manger = [AFNetworkReachabilityManager sharedManager];
    switch (manger.networkReachabilityStatus) {
        case AFNetworkReachabilityStatusUnknown:
          text = @"未知网络";
          break;
        case AFNetworkReachabilityStatusReachableViaWWAN:
          text = @"蜂窝网络";
          break;
        case AFNetworkReachabilityStatusReachableViaWiFi:
          text = @"wifi";
          break;
        case AFNetworkReachabilityStatusNotReachable:
        default:
          text = @"没有网络";
          break;
    }
    font = [UIFont boldSystemFontOfSize:20.0];
    
    if (font) [attributes setObject:font forKey:NSFontAttributeName];
    if (paragraph) [attributes setObject:paragraph forKey:NSParagraphStyleAttributeName];
    
    NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:text attributes:attributes];
    
    return attributedString;
}
#pragma mark 背景button
- (NSAttributedString *)descriptionForEmptyDataSet:(UIScrollView *)scrollView
{

    NSString *text = nil;
    UIFont *font = nil;
    NSMutableDictionary *attributes = [NSMutableDictionary new];
    NSMutableParagraphStyle *paragraph = [NSMutableParagraphStyle new];
    paragraph.lineBreakMode = NSLineBreakByWordWrapping;
    paragraph.alignment = NSTextAlignmentCenter;
    text = @"点击或下拉刷新";
    font = [UIFont boldSystemFontOfSize:15.0];

    if (font) [attributes setObject:font forKey:NSFontAttributeName];
    if (paragraph) [attributes setObject:paragraph forKey:NSParagraphStyleAttributeName];

    NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:text attributes:attributes];

    return attributedString;
}
#pragma mark 背景图
- (UIImage *)imageForEmptyDataSet:(UIScrollView *)scrollView
{
    if (self.isLoading) {
        return [UIImage imageNamed:@"loading_imgBlue_78x78"];
    }
    else {
        return [UIImage imageNamed:@"placeholder_remote"];
    }
}
#pragma mark 背景色
- (UIColor *)backgroundColorForEmptyDataSet:(UIScrollView *)scrollView
{
    if ([Team9EveningType shareEvening].eveningFailling) {
        return [UIColor colorWithWhite:0.15 alpha:1];
    } else return [UIColor colorWithWhite:0.95 alpha:1];
}
#pragma mark 未用 背景button 图
- (UIImage *)buttonBackgroundImageForEmptyDataSet:(UIScrollView *)scrollView forState:(UIControlState)state
{
    NSString *imageName;
    if (state == UIControlStateNormal) imageName = @"button_normal";
    if (state == UIControlStateHighlighted) imageName = @"button_highlight";
    
    UIEdgeInsets capInsets = UIEdgeInsetsMake(10.0, 10.0, 10.0, 10.0);
    UIEdgeInsets rectInsets = UIEdgeInsetsZero;

    return [[[UIImage imageNamed:imageName] resizableImageWithCapInsets:capInsets resizingMode:UIImageResizingModeStretch] imageWithAlignmentRectInsets:rectInsets];
}
#pragma mark 背景动画
- (CAAnimation *)imageAnimationForEmptyDataSet:(UIScrollView *)scrollView
{
    CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"transform"];
    animation.fromValue = [NSValue valueWithCATransform3D:CATransform3DIdentity];
    animation.toValue = [NSValue valueWithCATransform3D: CATransform3DMakeRotation(M_PI_2, 0.0, 0.0, 1.0) ];
    animation.duration = 0.25;
    animation.cumulative = YES;
    animation.repeatCount = MAXFLOAT;
    
    return animation;
}
#pragma mark 展示emptySet
- (BOOL)emptyDataSetShouldDisplay:(UIScrollView *)scrollView
{
    return YES;
}
#pragma mark 可点
- (BOOL)emptyDataSetShouldAllowTouch:(UIScrollView *)scrollView
{
    return YES;
}
#pragma mark 可滚
- (BOOL)emptyDataSetShouldAllowScroll:(UIScrollView *)scrollView
{
    return YES;
}
#pragma mark 是否有动画 这个可改变 刷新就变
- (BOOL)emptyDataSetShouldAnimateImageView:(UIScrollView *)scrollView
{
    return self.isLoading;
}
#pragma mark 点击
- (void)emptyDataSet:(UIScrollView *)scrollView didTapView:(UIView *)view
{
    self.loading = YES;
    [self.scrollView.header beginRefreshing];
}
#pragma mark 滚动
- (void)emptyDataSet:(UIScrollView *)scrollView didTapButton:(UIButton *)button
{
    self.loading = YES;
    [self.scrollView.header beginRefreshing];
}
#pragma mark - View Auto-Rotation

- (UIInterfaceOrientationMask)supportedInterfaceOrientations
{
    return UIInterfaceOrientationMaskAll;
}

- (BOOL)shouldAutorotate
{
    return NO;
}

@end
